package com.hitqz.robot.biz.flow.component.agv;

import com.alibaba.fastjson.JSONObject;
import com.hitqz.robot.api.business.model.dto.FlowContextDto;
import com.hitqz.robot.api.business.model.dto.FlowPatrolPreAndEndTicketsDto;
import com.hitqz.robot.api.common.entity.TicketsTask;
import com.hitqz.robot.biz.flow.util.LiteFlowUtil;
import com.yomahub.liteflow.annotation.LiteflowComponent;
import com.yomahub.liteflow.core.NodeComponent;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.client.RestTemplate;

import java.util.Optional;

/**
 * @author xupkun
 * @date 2025/5/11
 */
@Slf4j
@LiteflowComponent(value = "AgvNotifyGoodsFinish", name = "[轮对/轮轴]通知完成缓存架操作")
@AllArgsConstructor
public class AgvNotifyGoodsFinish extends NodeComponent {

    private final RestTemplate restTemplate;

    private final String url = "http://127.0.0.1:9999/admin/agv/updateRealStore?bindPosName=01N01&num=01N03";

    @Override
    public void process() throws Exception {
        LiteFlowUtil.checkInterruptFlag();
        FlowContextDto dto =  this.getRequestData();
        TicketsTask task = dto.getTicketsTask();
        log.info("任务数据 {}", task.getParseOrderParams());
        FlowPatrolPreAndEndTicketsDto ticketsDto = JSONObject.parseObject(dto.getTicketsTask()
                .getParseOrderParams(), FlowPatrolPreAndEndTicketsDto.class);
        String source = Optional.ofNullable(ticketsDto.getModalData().get("sourcePosName"))
                .orElse(ticketsDto.getModalData().get("sourcePosCode")).toString();
        String targetName = Optional.ofNullable(ticketsDto.getModalData().get("targetPosName"))
                .orElse(ticketsDto.getModalData().get("targetPosCode")).toString();
        String srcnum = String.valueOf(ticketsDto.getModalData().get("srcnum"));
        String pjData = String.valueOf(ticketsDto.getModalData().get("pjData"));
        String zx = String.valueOf(ticketsDto.getModalData().get("zx"));

        String uri = url.replace("01N01", targetName).replace("01N03", srcnum);
        String result = restTemplate.postForObject(uri, "", String.class);
        log.info("通知完成");

    }

}
